package com.sinosoft.ptas.control;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinosoft.ptas.control.dto.AnnualTargetData;
import com.sinosoft.ptas.entity.AnnualTargetEntity;
import com.sinosoft.ptas.service.AnnualTargetService;
import com.sinosoft.ptas.system.commom.CommonConstant;
import com.sinosoft.ptas.system.commom.ExceptionConstant;
/**
 * 年度目标控制层
 * @author lianWuJun
 * @time   2017年2月21日 上午10:12:10
 */
@Controller
@RequestMapping(value="annualTarget")
public class AnnualTargetController {
	
	@Autowired
	AnnualTargetService annualTargetService;
	
	/**
	 * 创建年度目标
	 * @param annualTargetData
	 * @return 年度目标传输类
	 */
	@RequestMapping(value="/createAnnualTarget",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData createAnnualTarget(AnnualTargetData annualTargetData){
		if(annualTargetData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		AnnualTargetEntity annualTargetEntity=annualTargetData.toEntity();
		try{
			String id=annualTargetService.createAnnualTarget(annualTargetEntity);
			annualTargetData.setId(id);//添加成功后，返回一个实体编号
			return new ResponseData(true,annualTargetData);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),null);	
		}
	}
	
	/**
	 * 根据年度目标编号查询年度目标
	 * @param id
	 * @return 年度目标传输类
	 */
	@RequestMapping(value="/retrieveAnnualTarget",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveAnnualTarget(String id){
		if(StringUtil.isBlank(id)){
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		AnnualTargetEntity annualTargetEntity=annualTargetService.retrieveAnnualTarget(id);
		AnnualTargetData annualTargetData=toannualTargetData(annualTargetEntity);
		return new ResponseData(true,annualTargetData);	
	}
	
	/**
	 * 根据年度目标类的属性查询年度目标
	 * @param annualTargetData
	 * @return 年度目标传输类集合
	 */
	@RequestMapping(value="/retrieveAnnualTargets",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveAnnualTargets(AnnualTargetData annualTargetData){
		AnnualTargetEntity annualTargetEntity=annualTargetData.toEntity(); 
		List<AnnualTargetEntity> annualTargetEntitys=annualTargetService.retrieveAnnualTargets(annualTargetEntity);
		List<AnnualTargetData> annualTargetDatas=toAnnualTargetDatas(annualTargetEntitys);
		return new ResponseData(true,annualTargetDatas);	
	}
	
	/**
	 * 根据年度目标编号更新年度目标
	 * @param annualTargetData
	 * @return 年度目标传输类
	 */
	@RequestMapping(value="/updateAnnualTarget",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData updateAnnualTarget(AnnualTargetData annualTargetData){
		if(annualTargetData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		AnnualTargetEntity annualTargetEntity=annualTargetData.toEntity();
		try{
			annualTargetService.updateAnnualTarget(annualTargetEntity);
			return new ResponseData(true,annualTargetData);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),null);
		}		
	}
	
	/**
	 * 删除年度目标
	 * @param id
	 */
	@RequestMapping(value="/deteleAnnualTargetById",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deteleAnnualTargetById(String id){
		if(StringUtil.isBlank(id)){
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		try{
			annualTargetService.deleteAnnualTarget(id);
			return new ResponseData(true,CommonConstant.DELETE_SUCCESS);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),null);	
		}
	}
	
	/**
	 * 年度目标实体类集合转换为传输类集合 
	 * @param annualTargetEntitys
	 * @return 年度传输类集合
	 */
	private List<AnnualTargetData> toAnnualTargetDatas(List<AnnualTargetEntity> annualTargetEntitys) {
		int size = annualTargetEntitys.size();
		List<AnnualTargetData> annualTargetDatas = new ArrayList<AnnualTargetData>();
		for (int i = 0; i < size; i++) { // 循环将实体数据转换为传输数据
			AnnualTargetData annualTargetData = new AnnualTargetData();
			annualTargetData.toannualTargetData(annualTargetEntitys.get(i));
			annualTargetDatas.add(annualTargetData);
		}
		return annualTargetDatas;
	}

	/**
	 * 年度目标实体类和传输类的转换
	 * @param annualTargetEntity
	 * @return 年度目标传输类
	 */
	private AnnualTargetData toannualTargetData(AnnualTargetEntity annualTargetEntity) {
		AnnualTargetData annualTargetData = new AnnualTargetData();
		annualTargetData.toannualTargetData(annualTargetEntity);
		return annualTargetData;
	}
}
